﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class SDTAB0305 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        SDTAB0305Data m_Data, m_DataX;
        public DataSet dsParamaters;
        SDTAB0305Report m_rpt;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        #region=====VKPOS=======
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        string KRS
        {
            get
            {
                return m_Data.Right("  " + txtABPAUFKRS.Text.ToString().Trim(), 2);
            }
        }
        string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtABPAUFLFD.Text.ToString().Trim(), 6); ;
            }
        }
        #endregion
        public SDTAB0305()
        {
            InitializeComponent();
            this.Name = "生产进展";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new SDTAB0305Data(OracleDB, proxy);
            //txtABPAUFKRS.Text = "FB";
            //txtABPAUFLFD.Text = "002390";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet Result = new DataSet();
                string FACTORY = txtFACTORY.Text.ToString().Trim();
                string STAFF = txtSTAFF.Text.ToString().Trim(); 
                if (FACTORY == "")
                {
                    FACTORY = "AA";
                }
                if(STAFF == "")
                {
                    STAFF = "AAAAAAAA";
                }
                DataRow dwABPOS = SetData.ABPOS.NewRow();
                dwABPOS["ABPMANDANT"] = "00";
                dwABPOS["ABPAUFKRS"] = KRS;
                dwABPOS["ABPAUFLFD"] = LFD;
                dwABPOS["ABPANRKR"] = FACTORY;
                dwABPOS["ABPAENDBEN"] = STAFF;
                Result = m_Data.BuildData(dwABPOS);
                SetData.Clear();
                SetData.Merge(Result);
                if (vwABPOS != null)
                {
                    if (vwABPOS.View != null)
                    {
                        vwABPOS.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

            DataSet setRpt = new DataSet();

            if (setRpt == null)
                return;
            m_DataX = new SDTAB0305Data(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            if (m_DataX.exAgent._app == null)
            {
                m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
            }
            m_rpt = new SDTAB0305Report(dicParamater);
            m_rpt.dsSource = SetData;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }
        private void MDECKDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            try
            {
                return;
                #region=======更新===========
                if (e.EditAction != DataGridEditAction.Commit)
                    return;

                DataGridRow row = e.Row;
                DataRow rowmu = SetData.ABAUFM.NewRow();
                string krs = ((DataRowView)row.Item)["ABPAUFKRS"].ToString();
                string lfd = ((DataRowView)row.Item)["ABPAUFLFD"].ToString();
                string pos = ((DataRowView)row.Item)["ABPAUFPOS"].ToString();

                var query = from DataRow rowx in SetData.Tables["ABPOS"].AsEnumerable()
                            where rowx["ABPAUFKRS"].ToString() == krs
                            && rowx["ABPAUFLFD"].ToString() == lfd
                            && rowx["ABPAUFPOS"].ToString() == pos
                            select rowx;
                foreach (DataRow rowx in query)
                {
                    rowmu["ABAMANDANT"] = "00";
                    rowmu["ABAAUFKRS"] = rowx["ABPAUFKRS"].ToString();
                    rowmu["ABAAUFLFD"] = rowx["ABPAUFLFD"].ToString();
                    rowmu["ABAAUFPOS"] = rowx["ABPAUFPOS"].ToString();
                    rowmu["ABAAPOS"] = "0001";
                    rowmu["ABABEM1"] = m_Data.Left(rowx["ABABEM1"].ToString().TrimStart() + "                                                                ", 60);//m_Data.Left(m_Data.ConvertGB2312To8859P1(rowx["ABABEM1"].ToString().TrimStart()) + "                                                                ", 60);
                    rowmu["ABABEM2"] = m_Data.Left(rowx["ABABEM2"].ToString().TrimStart() + "                                                                ", 60); //m_Data.Left(m_Data.ConvertGB2312To8859P1(rowx["ABABEM2"].ToString().TrimStart()) + "                                                                  ", 60);
                    m_Data.UpdateABAUFM(rowmu);
                }
                #endregion

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            DataRow rowab =((DataRowView) (vwABPOS.View.CurrentItem)).Row as DataRow;
            DataRow rowmu = SetData.ABAUFM.NewRow();
            rowmu["ABAMANDANT"] = "00";
            rowmu["ABAAUFKRS"] = rowab["ABPAUFKRS"].ToString();
            rowmu["ABAAUFLFD"] = rowab["ABPAUFLFD"].ToString();
            rowmu["ABAAUFPOS"] = rowab["ABPAUFPOS"].ToString();
            rowmu["ABAAPOS"] = "0001";
            rowmu["ABABEM1"] = m_Data.Left(rowab["ABABEM1"].ToString().TrimStart() + "                                                                ", 60);//m_Data.Left(m_Data.ConvertGB2312To8859P1(rowx["ABABEM1"].ToString().TrimStart()) + "                                                                ", 60);
            rowmu["ABABEM2"] = m_Data.Left(rowab["ABABEM2"].ToString().TrimStart() + "                                                                ", 60); //m_Data.Left(m_Data.ConvertGB2312To8859P1(rowx["ABABEM2"].ToString().TrimStart()) + "                                                                  ", 60);
            m_Data.UpdateABAUFM(rowmu); 
        }
    }
}
